<?php
/**
 * 【信誉控制器】
 * Created by PhpStorm.
 * User: sunkai
 * Date: 2018-06-07
 * Time: 15:20
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Support\Facades\DB;
use App\Http\model\Curdpost;

class CredibilityController extends Controller
{
    /**
     * 【司机信誉列表】
     * @Author sk
     * @DateTime 2018-06-05
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
       // $array = $request->all();
        $dat = $request->all();
        $array = array_splice($dat, 1);
        $select = [
            'credibility.driver_id',//司机ID
            'credibility.admin_user',//修改人
            'credibility.update_time',//更新时间
            'credibility.branch',//服务质量信誉考核机构
            'credibility.time_id',//信誉考核周期表ID
            'credibility.services_slippage',//服务质量
            'credibility.credibility_lv',//信誉等级5分
            'driver.licenseId',//驾驶证号码
            'driver.driver_name',//司机姓名
            'driver.driver_phone',//司机手机号
            'driver.sex',//性别 :1男 2女
            'driver.convoy_id',//车队id
            'driver.car_id',//车辆id
            'car_car.num',//车牌号
            'car_car.typeid',//车系id
            'car_series.name as sname',//车系名称
            'convoy.name',//车队名称
            'convoy.branch_id',
            'branch.company_name',//机构名称
            'branch.type'//机构类型
        ];
        $handle = DB::table('credibility')
            ->join('driver', 'credibility.driver_id', '=', 'driver.id')//差司机
            ->join('convoy', 'driver.convoy_id', '=', 'convoy.id')//车队
            ->join('branch', 'convoy.branch_id', '=', 'branch.id')//机构
            ->join('car_car', 'driver.car_id', '=', 'car_car.id')//车牌号
            ->join('car_series', 'car_car.typeid', '=', 'car_series.id')//车系
            ->join('credibility_time', 'credibility.time_id', '=', 'credibility_time.id')
            ->select($select)
            ->orderBy('credibility.update_time', 'desc');
        foreach ($array as $key => $val) {
            $array[$key] && $handle->where($key, 'like', '%' . $val . '%');
        }
        $datas = $handle->get()->toarray();

        /* $where='LicenseId='.$datas['licenseId'];
         if ($datas){
             $all = ['CompanyId'=>'test','Licenseld'=> $datas['licenseId'],'Level'=>$datas['credibility_lv'],'TestDate'=>date("Ymd",$datas['update_time']),'TestDepartment'=>$datas['branch']];
             $news = new Curdpost();
             $news->NewUpdate("rateddriverpunish",$all,$where);
         }*/
        $data = json_decode(json_encode($datas), true);
        return $data ? response(['data' => $data, 'code' => 1000, 'message' => '信息返回成功！'], 200) : response(['data' => '', 'code' => 1011, 'message' => '信息返回失败！'], 400);

    }

    /**
     * 【司机信誉修改考核zhouqi】
     * @Author sk
     * @DateTime 2018-06-08
     * @param Request $request
     * @return array
     */
    public function save(Request $request)
    {
        $data = $request->all();
        if (isset($data['s'])) {
            $data = array_except($data, ['s']);
        }
        /*if (empty($arr['type']) || empty($arr['time'])) {
            return '缺少参数';
        }*/
        //dd($data);
        $d = DB::table('credibility_time')->update($data);
        $data = array();
        if ($d) {
            $data = DB::table('credibility_time')->first();
            $data = json_decode(json_encode($data), true);
        }
        return $data ? response(['data' => $data, 'code' => 1000, 'message' => '修改成功！'], 200) : response(['data' => '', 'code' => 1011, 'message' => '修改失败！'], 400);
    }

    /**
     * 【司机信誉修改考核信息】
     * @Author sk
     * @DateTime 2018-06-08
     * @param Request $request
     * @return array
     */
    public function reset(Request $request)
    {
        $array = $request->all();


    }
}